package com.atguigu.gulimall.product.web;

import com.alibaba.fastjson.JSON;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.webvo.CategoryLevel2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping({"/","/index.html"})
    public String index(Model model){
        List<CategoryEntity> category1Level = categoryService.getCategory1Level();
        model.addAttribute("categorys",category1Level);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public String getCatelogJson(){
        Map<String, List<CategoryLevel2Vo>> catelogJson = categoryService.getCatelogJson();
        return JSON.toJSONString(catelogJson);
    }

    @ResponseBody
    @GetMapping("/testRedisson")
    public String testRedisson(){
        RLock lock = redissonClient.getLock("my-lock");
        //加锁
        lock.lock();
        try {
            System.out.println("加锁成功：" + Thread.currentThread().getId());
            Thread.sleep(30000);
        } catch (InterruptedException e) {

        } finally {
            //释放锁
            System.out.println("锁释放：" + Thread.currentThread().getId());
            lock.unlock();
        }

        return "hello redisson";
    }

    //写锁
    @GetMapping("/write")
    @ResponseBody
    public String writeValue(){
        //读写锁是同一把锁
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        RLock writeLock = lock.writeLock();
        String uuid = "";
        try {
            uuid = UUID.randomUUID().toString();
            writeLock.lock();
            System.out.println("写锁占用成功");
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("writeValue", uuid);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //写锁释放，读锁才能使用
            writeLock.unlock();
            System.out.println("写锁释放完成");
        }

        return uuid;
    }

    //读锁
    @GetMapping("/read")
    @ResponseBody
    public String readValue(){
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        RLock readLock = lock.readLock();
        String uuid = "";
        try {
            readLock.lock();
            uuid = redisTemplate.opsForValue().get("writeValue");
            System.out.println("读锁占用成功");
            Thread.sleep(30000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readLock.unlock();
            System.out.println("读锁释放完成");
        }

        return uuid;

    }


}
